package com.canbot.u05.activity.base

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.alibaba.fastjson.JSON
import com.canbot.u05.R
import com.canbot.u05.activity.BaseActivityNoTitle
import com.canbot.u05.activity.free.MediaInfo
import com.canbot.u05.bean.Audio
import com.canbot.u05.bean.JsonResponse
import com.canbot.u05.socket.IpMsgConst
import com.canbot.u05.socket.MsgSendUtils
import com.canbot.u05.socket.MsgType
import com.canbot.u05.utils.Logger
import com.canbot.u05.utils.OkHttpUtils
import kotlinx.android.synthetic.main.base_audio_item.view.*
import kotlinx.android.synthetic.main.base_audio_list_activity.*
import java.util.HashMap

/**
 * Created by zmp on 2019/6/3 16:57
 * @author zmp
 * 音频选择界面
 */
class BaseAudioListActivity : BaseActivityNoTitle() {


    private lateinit var myAdapter: MyAdapter
    private var currentPosition: Int = -1

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.base_audio_list_activity)
        initView()
        getData()
    }

    private fun getData() {
        showBaseLoadDialog()
        val url = "http://" + IpMsgConst.HEAD_SOCKET_HOST + ":7755/getAudioList"
        val hashMap = HashMap<String, String>()
        hashMap["path"] = "/uusound"
        OkHttpUtils.upLoadFile(url, hashMap, object : OkHttpUtils.OkHttpCallBack {
            override fun onSuccess(result: String) {
                Log.e("t", result)
                dismissBaseDialog()
                val strMsgBean = JSON.parseObject(result, JsonResponse::class.java)
                Logger.e("save", "onSuccess:" + "http://" + IpMsgConst.HEAD_SOCKET_HOST + ":7755/" + "strMsgBean.getRc()" + strMsgBean.rc)
                if (strMsgBean.rc == 0) {
                    runOnUiThread {
                        val list = JSON.parseArray(strMsgBean.result.toString(), Audio::class.java)
                        arr.clear()
                        arr.addAll(list)
                        myAdapter.notifyDataSetChanged()
                    }
                } else {
                    showBaseHintDialog(getString(R.string.no_voice_find), true)
                }
            }

            override fun onFailure() {
                dismissBaseDialog()
                showBaseHintDialog(getString(R.string.service_error_hint), true)
            }
        })

    }

    val arr: ArrayList<Audio> = ArrayList()


    private fun initView() {
        back_btn_head.setOnClickListener {
            finish()
        }
        saveTextView.setOnClickListener {
            if (currentPosition != -1) {
                setResult(Activity.RESULT_OK, Intent()
                        .putExtra(BaseVoiceSetActivity.AUDIO_TYPE_KEY, MediaInfo.AUDIO_TYPE_AUDIO)
                        .putExtra(BaseVoiceSetActivity.AUDIO_PATH_OR_CONTENT, arr[currentPosition].path)
                        .putExtra(BaseVoiceSetActivity.AUDIO_SIMPLE_NAME_KEY, arr[currentPosition].name))
                finish()
            } else {
                showBaseHintDialog(getString(R.string.no_voice_source))
            }
        }

        voiceRecycleView.layoutManager = LinearLayoutManager(this)
        myAdapter = MyAdapter(this, arr)
        voiceRecycleView.adapter = myAdapter
    }


    internal inner class MyAdapter(context: Context, var arr: ArrayList<Audio>) : RecyclerView.Adapter<MyHolder>() {

        private val layoutInflater: LayoutInflater = LayoutInflater.from(context)

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyHolder {
            val inflate = layoutInflater.inflate(R.layout.base_audio_item, parent, false)
            return MyHolder(inflate)
        }

        override fun onBindViewHolder(holder: MyHolder, position: Int) {
            val str = arr[position]
            holder.updateView(str)
        }

        override fun getItemCount(): Int {
            return arr.size
        }

    }

    internal inner class MyHolder(itemView: View) : RecyclerView.ViewHolder(itemView), View.OnClickListener {

        private var audioTextView: TextView = itemView.audioTextView
        private var audioCheckIv: ImageView = itemView.audioCheckIv

        init {
            itemView.setOnClickListener(this)
        }

        override fun onClick(v: View) {
            if (currentPosition != adapterPosition) {
                currentPosition = adapterPosition
                MsgSendUtils.sendStringMsg(MsgType.PLAY_SOUND,arr[currentPosition].path)
                myAdapter.notifyDataSetChanged()
            }
        }

        fun updateView(str: Audio) {
            audioTextView.text = str.name
            if (currentPosition != -1) {
                audioCheckIv.visibility = if (str.path == arr[currentPosition].path) View.VISIBLE else View.GONE
            }
        }

    }

}